import requests
import time
import hashlib


def make_md5(text):
    """
    Md5加密 只能加密字符串
    :param text:  需要加密的内容，必须是个字符串
    :return:
    """
    m = hashlib.md5() # 创建一个 md5加密工具
    m.update(str(text).encode(encoding='utf-8'))
    res = m.hexdigest()  # 生成加密后的内容
    return res


def make_username():
    username = int(time.time() * 10000)
    return username


s = requests.session()
username = make_username()
regUrl = 'http://114.132.200.42:3456/user/register'
mydata = {
    "username": f"xi{username}",
    "password": "xixi123456",
    "cpassword": "xixi123456",
    "checkcode": "1234",
    "email": f'{username}@qq.com'
}
res = s.post(url=regUrl, data=mydata)
# print(res.status_code)
# print(res.json())

loginUrl = 'http://114.132.200.42:3456/user/login'
logindata = {
"username":f"xi{username}",
"password":make_md5('xixi123456'),
"checkcode":"1234"
}
res1 = s.post(url=loginUrl, data=logindata)
a = res1.json()
myuserid = (a['userId'])
myusertoken = (a['userToken'])

createurl = 'http://114.132.200.42:3456/task/info'
mydata ={"title": '我的任务123'}
head = {'userId': myuserid, 'userToken': myusertoken}
res2 = s.post(url=createurl, data=mydata, headers=head)
b = res2.json()
mytaskId = (b['taskid'])

Geturl = 'http://114.132.200.42:3456/task/info'
mydata = {"status": "0"}
res3 = s.get(url=Geturl, headers=head, params=mydata)
print(res3.json())

successUrl = 'http://114.132.200.42:3456/task/info'
mydata = {"task_id": mytaskId}
res4 = s.put(url=successUrl, headers=head, data=mydata)
print(res4.json())

deleteUrl = 'http://114.132.200.42:3456/task/info'
mydata = {"task_id": mytaskId}
res5 = s.delete(url=deleteUrl,headers=head, data=mydata)
print(res5.json())

imgUrl = 'http://114.132.200.42:3456/user/uploal_action'
myimg = [
    ('pic', ('波吉.jpg', open('波吉.jpg', 'rb')))
]
res6 = s.post(url=imgUrl, headers=head, files=myimg)
print(res6.json())
